package myky.com.doctorapp.presenter

import android.annotation.SuppressLint
import android.util.Log
import com.xiangyao.train.utils.RxScedulerHelper

import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import myky.com.doctorapp.base.BasePresenterIm
import myky.com.doctorapp.base.RxSubscribe
import myky.com.doctorapp.contract.HealthRecordActivityContract
import myky.com.doctorapp.entity.HealthRecordEntity
import myky.com.doctorapp.model.HealthRecordActivityModel


/**
 * Description:
 *
 * @author wuyangwuyang
 * Created by 2019/3/13 on 17:55
 */
class HealthRecordActivityPresenter : BasePresenterIm<HealthRecordActivityContract.View>(), HealthRecordActivityContract.Presenter {

    var healthRecordActivityModel: HealthRecordActivityModel = HealthRecordActivityModel()

    var healcontract: List<HealthRecordEntity.PerProfileBean.FirstContactsListsBean> = ArrayList()
    var operation: List<HealthRecordEntity.PerProfileBean.OperationListsBean> = ArrayList()




    override fun pfindHealthRecord() {
        var observable = healthRecordActivityModel.findHealthRecord()
        var observable1 = healthRecordActivityModel.healthContact()
        var observable2 = healthRecordActivityModel.healthOperation()
        observable1.subscribeOn(Schedulers.io())
                .filter {
                    //是否是联系人判断
                    it.type == 245
                }
                .flatMap {
                    var firstContactsLists = it.payLoad.firstContactsLists
                    if (firstContactsLists != null) {
                        healcontract = firstContactsLists
                    }
                    observable2
                }
                .filter {
                    //是否是手术记录列表
                    it.type == 241
                }
                .flatMap {

                    var operationLists = it.payLoad.operationLists
                    if (operationLists != null) {
                        operation = operationLists
                    }
                    observable
                }
                .filter {
                    it.type == 240
                }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : RxSubscribe<HealthRecordEntity>() {
                    override fun onSuccess(t: HealthRecordEntity) {

                        mView.hideLoading()
                        t.perProfile.firstContactsLists = healcontract
                        t.perProfile.operationLists = operation

                        mView.showContentView()
                        mView.showDataSuccess(t)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        mView.hideLoading()
                        mView.showNetErrorView()
                    }

                    override fun onSubscribe(d: Disposable) {
                        addSubscription(d)
                        mView.showLoadingView("请稍后")
                    }

                })

    }
}
